/*
 *    Webical - http://www.webical.org
 *    Copyright (C) 2007 Func. Internet Integration
 *
 *    This file is part of Webical.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.webical.web.component.event;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.webical.Event;
import org.webical.web.action.IAction;
import org.webical.web.component.AbstractBasePanel;
import org.webical.web.component.behavior.FormComponentValidationStyleBehavior;
import org.webical.web.event.ExtensionPoint;

/**
 * Creates an add/edit for for an event depending on whether a Event is provided or null
 * @author jochem
 * @author Mattijs Hoitink
 */

public abstract class EventFormPanel extends AbstractBasePanel {
	private static final long serialVersionUID = 1L;
	public  static final String FORM_EXTENSIONS_MARKUP_ID = "formExtensions";
	private static final String EVENT_ADD_EDIT_FORM_MARKUP_ID = "eventForm";

	private GregorianCalendar date = new GregorianCalendar();

	private Event editEvent;
	private IModel eventModel;

	/**
	 * Constructor
	 * @param markupId The ID used in the markup
	 * @param event The Event to be edited or null when a new event is edited
	 * @param date
	 */
	public EventFormPanel(String markupId, Event editEvent, Calendar date) {
		super(markupId, EventFormPanel.class);

		// set variables needed the form
		this.editEvent = editEvent;
		this.date.setTime(date.getTime());
	}

	public void setupCommonComponents() {
		// Create a new EventForm
		EventForm eventForm = new EventForm(EVENT_ADD_EDIT_FORM_MARKUP_ID, editEvent, this.date) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onAction(IAction action) {
				EventFormPanel.this.onAction(action);
			}

		};
		eventForm.add(new FormComponentValidationStyleBehavior());

		//Create and register a new extensionpoint for plugins
		RepeatingView extensionPoint = new RepeatingView(FORM_EXTENSIONS_MARKUP_ID);
		getExtensionPoints().put(FORM_EXTENSIONS_MARKUP_ID, new ExtensionPoint(extensionPoint, eventModel));

		//Add the components
		eventForm.add(extensionPoint);
		addOrReplace(eventForm);
	}

	public void setupAccessibleComponents() {
		// NOT IMPLEMENTED
	}

	public void setupNonAccessibleComponents() {
		// NOT IMPLEMENTED
	}

	/**
	 * Handle actions generated by this panel
	 * @param action The action to handle
	 */
	public abstract void onAction(IAction action);

}
